package com.czapl.drawing;

import com.czapl.game3.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tile extends View{
	private static Tile selectedTile = null; 
	private int w, h;
	public int type;
	private Bitmap bm = null;
	private Tower tower = null;
	private final int FRAME_BORDER = 1;
	Paint myPaint = null;
	public Tile(Context context, CharSequence txt) {
		super(context);
                
		bm = BitmapFactory.decodeResource(getResources(), R.drawable.grass1);
		w = bm.getWidth()+FRAME_BORDER;
		h = bm.getHeight()+FRAME_BORDER;
		myPaint = new Paint();
		myPaint.setColor(Color.rgb(255, 255, 255));
		myPaint.setAlpha(50); 
		myPaint.setStrokeWidth(1);
		
		setOnClickListener(new View.OnClickListener() {
		    @Override
		    public void onClick(View v) {
		    	Log.d("TASIA", "Tasinska");
		    	if((null == selectedTile) || (Tile.this != selectedTile) ) {
		    		selectedTile = Tile.this;	
		    	}
		    	else {
		    		selectedTile = null;
		    	}
		    }
		});

	}
	
	public void placeTower(Tower tower)
	{
		this.tower = tower;
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
        final int desiredWSpec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
        final int desiredHSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
        this.setMeasuredDimension(desiredWSpec, desiredHSpec);
        super.onMeasure(desiredWSpec, desiredHSpec);
	    
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
	    //Log.v("Tile", "l="+l+" t="+t+" r="+r+" b="+b);
	   	
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
	   //super.onDraw(canvas);
	   canvas.drawBitmap(bm, 0, 0, null);
	   if(null != tower) {
		   tower.draw(canvas);
	   }
		if(this == selectedTile) {
			canvas.drawRect(0, 0, getWidth()-FRAME_BORDER, getHeight()-FRAME_BORDER, myPaint);
		}
		
		
	}
	 
}
